import type { ResponseData } from '@/utils/types.d';
import { SUCCESS_CODE } from '@/utils/types.d';
import request from '@/utils/request';
import type {
  ArrearOrderTableItem,
  CancelOrderParams,
  CancelRecordItem,
  ChangePlateParams,
  ChangeTimeParams,
  OutOrderParams,
  ParkingOrderDetail,
  ParkingOrderTableItem,
  PayFlowTableItem,
  PaymentRecordTableItem,
  RefundParam,
} from './types.d';

export async function queryParkingOrders(
  params: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: ParkingOrderTableItem[];
}> {
  const res: ResponseData<{
    total: number;
    data: ParkingOrderTableItem[];
  }> = await request('/api/parking-order/v1.0.0/orders', {
    method: 'GET',
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function queryArrearOrders(
  params: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: ArrearOrderTableItem[];
}> {
  const res: ResponseData<{
    total: number;
    list: ArrearOrderTableItem[];
  }> = await request('/api/arrear-order/v1.0.0/arrear-orders', {
    method: 'GET',
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.list };
  return { success: false, total: 0, data: [] };
}
export async function getParkingOrder(orderNo: string): Promise<{
  success: boolean;
  message: string;
  data: ParkingOrderDetail;
}> {
  const res: ResponseData<ParkingOrderDetail> = await request(
    `/api/parking-order/v1.0.0/order/${orderNo}`,
  );
  if (res?.code === SUCCESS_CODE) return { success: true, message: '获取成功', data: res.data };
  return { success: false, message: res?.message || '获取失败', data: res.data };
}

export async function refund(data: RefundParam) {
  const res = await request('/api/refund-record/v1.0.0/refund', {
    method: 'POST',
    data,
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '申请成功',
    };
  return { data: res?.data, success: false, message: res?.message || '申请失败' };
}
export async function updateRefund(payNo: string) {
  const res = await request(`/api/refund-record/v1.0.0/update/${payNo}`, {
    method: 'POST',
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '更新成功',
    };
  return { data: res?.data, success: false, message: res?.message || '更新失败' };
}

export async function exportParkingOrders(params: {
  ownerId?: string;
  parkingLotId?: string;
  inTime?: number[];
  outTime?: number[];
}) {
  return await request('/api/parking-order/v1.0.0/excel', {
    params,
    responseType: 'blob',
  });
}

export async function importArrearOrder(form: FormData) {
  const res = await request('/api/arrear-order/v1.0.0/import', {
    method: 'POST',
    requestType: 'form',
    data: form,
    body: form,
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: res.message || '导入成功',
    };
  return { data: res?.data, success: false, message: res?.message || '导入失败' };
}

export async function changeOrderPlate(data: ChangePlateParams) {
  const res = await request('/api/parking-order/v1.0.0/change/plate', {
    method: 'POST',
    data,
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '修改成功',
    };
  return { data: res?.data, success: false, message: res?.message || '修改失败' };
}

export async function cancelOrder(data: CancelOrderParams, isArrear: boolean) {
  const res = await request(
    isArrear ? '/api/arrear-order/v1.0.0/cancel/order' : '/api/parking-order/v1.0.0/cancel/order',
    {
      method: 'PUT',
      data,
    },
  );
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '作废成功',
    };
  return { data: res?.data, success: false, message: res?.message || '作废失败' };
}

export async function queryCancelRecords(
  params: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: CancelRecordItem[];
}> {
  const res: ResponseData<{
    total: number;
    list: CancelRecordItem[];
  }> = await request('/api/cancel-record/v1.0.0/list', {
    method: 'GET',
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.list };
  return { success: false, total: 0, data: [] };
}

export async function outOrder(data: OutOrderParams) {
  const res = await request('/api/parking-order/v1.0.0/out', {
    method: 'POST',
    data,
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '驶出成功',
    };
  return { data: res?.data, success: false, message: res?.message || '驶出失败' };
}

export async function fixOrderTime(data: ChangeTimeParams) {
  const res = await request('/api/parking-order/v1.0.0/fix/time', {
    method: 'POST',
    data,
  });

  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '修改成功',
    };
  return { data: res?.data, success: false, message: res?.message || '修改失败' };
}

export async function queryPaymentRecords(
  params: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: PaymentRecordTableItem[];
}> {
  const res: ResponseData<{
    total: number;
    data: PaymentRecordTableItem[];
  }> = await request('/api/parking-order/v1.0.0/payments', {
    method: 'GET',
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function queryPayFlows(
  params: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: PayFlowTableItem[];
}> {
  const res: ResponseData<{
    total: number;
    data: PayFlowTableItem[];
  }> = await request('/api/parking-order/v1.0.0/payflows', {
    method: 'GET',
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}
